<?php include_once('header.php'); ?>
<?php include_once('left_navigation.php'); ?>
<?php
	include('../classess/class_manage_user.php');

	$new_user = new manageUser();

	if(isset($_GET['del_id'])){
		$delete_activate_user = $new_user->del_user($_GET['del_id']); 
	}
	else if(isset($_GET['d']) && $_GET['d']=='1' && $_GET['act_id']!=''){
		$delete_activate_user = $new_user->remove_user($_GET['act_id']); 
	}
	else if(isset($_GET['act_id'])){
		$delete_activate_user = $new_user->activate_user($_GET['act_id']); 
	}
	else if(isset($_POST['pay_type_status'])){
		$delete_activate_user = $new_user->user_paid_type(); 
	}
?>
<div class="col-lg-12"><!-- Start Middle Content Div-->
	<div class="box">
		<div class="row">		
			<div class="col-lg-12">
				<div class="box">
					<div class="box-header" data-original-title>
						<h2><i class="fa fa-user"></i><span class="break"></span>Users</h2>
						<div class="box-icon">
							<a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
							<a href="table.html#" class="btn-close"><i class="fa fa-times"></i></a>
						</div>
					</div>
					<div class="box-content">
						<?php 
								if(isset($delete_activate_user)){	
							?>
							<div class="alert alert-info">
								<div style="float: right; width: 5%;"><button type="button" class="close" data-dismiss="alert">&times;</button></div>
								<div style="float:left;width: 92%;word-wrap: break-word;"><?php echo $delete_activate_user; ?></div>
								<div style="clear:both;"></div>							
							</div>
							<?php
								}
							?>
						<div class="search_box">
							<!--<div class="fl_left sel_label_search">Search:</div>
							<div class="fl_left" style="width:60%;">									
								<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?mng_user=1" id="search_form" name="search_form">
									<input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php if(isset($_POST['search_value'])){echo $_POST['search_value']; }else{ echo '';}  if(isset($_GET['srch_value'])){echo $_GET['srch_value']; }else{ echo '';}?>">&nbsp;
									<input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">
								</form>

							</div>
							<div class="clear_fix"></div>-->
							<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?mng_user=1" id="search_form" name="search_form">
								<div class="fl_left" style="width:50%;">
									<div class="fl_left sel_label_search" style="width:11%;">Search:</div>
									<div class="fl_left" style="width:86%;">	
										<input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php if(isset($_POST['search_value'])){echo $_POST['search_value']; }else{ echo '';}  if(isset($_GET['srch_value'])){echo $_GET['srch_value']; }else{ echo '';}?>" style="width:45%;">&nbsp;
										<input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">										
									</div>
									<div class="clear_fix"></div>
								</div>
								<div class="fl_right">
									<div class="fl_left" style="width:15%; margin-top:5px;">Number Of Records : </div>
									<div class="fl_left">
									<?php
										$limit_10='';
										$limit_25='';
										$limit_50='';
										$limit_100='';
										if(isset($_POST['sel_limit'])){
											/*if($_POST['sel_limit'] == '10'){
												$limit_10 = "selected='selected'";
											}*/
											if($_POST['sel_limit'] == '25'){
												$limit_25 = "selected='selected'";
											}
											if($_POST['sel_limit'] == '50'){
												$limit_50 = "selected='selected'";
											}
											if($_POST['sel_limit'] == '100'){
												$limit_100 = "selected='selected'";
											}
										}
										if(isset($_GET['sel_get_limit'])){
											/*if($_GET['sel_get_limit'] == '10'){
												$limit_10 = "selected='selected'";
											}*/
											if($_GET['sel_get_limit'] == '25'){
												$limit_25 = "selected='selected'";
											}
											if($_GET['sel_get_limit'] == '50'){
												$limit_50 = "selected='selected'";
											}
											if($_GET['sel_get_limit'] == '100'){
												$limit_100 = "selected='selected'";
											}
										}
									?>
									<select name="sel_limit" id="sel_limit" onchange="javascript:document.search_form.submit();">
										<!--<option value="10" <?php echo $limit_10; ?> >10</option>
										--><option value="25" <?php echo $limit_25; ?> >25</option>
										<option value="50" <?php echo $limit_50; ?> >50</option>
										<option value="100" <?php echo $limit_100; ?> >100</option>
									</select>
										
									</div>
									<div class="clear_fix"></div>
								</div>
								<div class="clear_fix"></div>							
							</form>
						</div>
						<table class="table table-striped table-bordered" style="background:#fff;">
						  <thead>
							  <tr>
								  <th>Name</th>
								  <th>Email</th>
								  <th>Contact Number</th>
								  <th>Address</th>
								  <th>City</th>
								  <th>State</th>
								  <th>Zip</th>
								  <th>Country</th>
								  <th>Course</th>
								  <th>Paid Status</th>
								  <th>Account Number</th>
								  <th>Bank Name</th>
								  <th>Date</th>
								  <th>Ebook</th>
								  <th>Ebook Download Length</th>
								  <th>Is Refund</th>
								  <th>Is Active</th>
								  <th>Actions</th>
							  </tr>
						  </thead>   
						  <tbody>
							<?php
								$rec_limit = 25;
								if(isset($_POST['sel_limit'])){
									$rec_limit = $_POST['sel_limit'];
								}

								if(isset($_GET['sel_get_limit'])){
									$rec_limit = $_GET['sel_get_limit'];
								}

								$condition_search = '';
								$value = '';
								if(isset($_POST['search_value'])){										
									$value = trim($_POST['search_value']);
									
									if(!empty($value)){
										$cond_where = ' AND';
										$condition_search = $cond_where." (b.name like '%".$value."%' OR a.email like '%".$value."%' OR b.phone like '%".$value."%')";
									}
								}

								if(isset($_GET['srch_value'])){	
									$value = $_GET['srch_value'];
									
									if(!empty($value)){
										$cond_where = ' AND';

										$condition_search = $cond_where." (b.name like '%".$value."%' OR a.email like '%".$value."%' OR b.phone like '%".$value."%')";
									}
								}

								$select_count_course = "SELECT count(a.id) FROM tbl_user as a, tbl_user_details as b WHERE b.user_id = a.id".$condition_search;
								$result_count_course = mysql_query($select_count_course);

								$row = mysql_fetch_array($result_count_course);
								$rec_count = $row[0];

								if( isset($_GET['page']) )
								{
								   $page = $_GET['page'] + 1;
								   $offset = $rec_limit * $page ;
								}
								else
								{
								   $page = 0;
								   $offset = 0;
								}

								$serial_number = $page*$rec_limit;
								
								$left_rec = $rec_count - ($page * $rec_limit);

								$select_trainer = "SELECT a.id as userId, a.*, b.id as userDetailId, b.* FROM tbl_user as a, tbl_user_details as b WHERE b.user_id = a.id ".$condition_search." ORDER BY a.id DESC LIMIT ".$offset.", ".$rec_limit;
								$query_trainer = mysql_query($select_trainer) or die(mysql_error());
								
								$i = 1;
								if(mysql_num_rows($query_trainer)>0){
									while($row = mysql_fetch_array($query_trainer)){
										$serial_number++;
										echo '
											<tr>
												<td>'.$row['name'].'</td>
												<td>'.$row['email'].'</td>
												<td>'.$row['phone'].'</td>
												<td>'.$row['address'].'</td>
												<td>'.$row['city'].'</td>
												<td>'.$row['state'].'</td>
												<td>'.$row['zip'].'</td>
												<td>'.$row['country'].'</td>
												<td>';
													if($row['is_ebook']){
														$sel_ebook = "select * from tbl_ebook where id='".$row['course_id']."'";
														$course_book = mysql_query($sel_ebook) or die(mysql_error());
														if(mysql_num_rows($course_book)>0){
															while($row_ebook = mysql_fetch_array($course_book)){
																echo $row_ebook['ebook_name'];
															}
														}else{
															echo 'No Course Availabale';
														}
													}else{
														$sel_course = "select * from tbl_courses where id='".$row['course_id']."'";
														$course_query = mysql_query($sel_course) or die(mysql_error());
														if(mysql_num_rows($course_query)>0){
															while($row_course = mysql_fetch_array($course_query)){
																echo $row_course['course_name'];
															}
														}else{
															echo 'No Course Availabale';
														}
													}
												echo '</td>
												<td>';
													if($row['via_paid'] == 'paypal' || $row['via_paid'] == '1'){
														echo $row['via_paid'];
													}else if($row['via_paid'] == '2' || $row['via_paid'] == '3'){
														echo '<form name="paidStatusForm'.$row['userDetailId'].'" id="paidStatusForm'.$row['userDetailId'].'" action="'.$_SERVER['PHP_SELF'].'?mng_user=1" method="POST">
															<input type="checkbox" name="sel_pay_type" id="sel_pay_type'.$row['userDetailId'].'" class="chk_sub_chkbox">
															<select name="pay_type_status" id="pay_type_status'.$row['userDetailId'].'" class="chkSelValue">
																<option value="">Select Type</option>
																';
																$sel_bank_status = "SELECT * FROM tbl_bank_status";
																$course_bank_status = mysql_query($sel_bank_status) or die(mysql_error());
																if(mysql_num_rows($course_bank_status)>0){
																	while($row_bank_status = mysql_fetch_array($course_bank_status)){
																		echo '<option value="'.$row_bank_status['id'].'"';
																		if($row_bank_status['id'] == $row['via_paid']){ echo 'selected="selected"';}
																		echo '>'.$row_bank_status['type'].'</option>';
																	}
																}
														echo '</select><input type="hidden" name="userdtlpayerId" id="userdtlpayerId" value="'.$row['userDetailId'].'"><input type="text" name="drft_chq_no" id="drft_chq_no" value="'.$row['cheque_draft_no'].'">
														</form>';
													
													}else if($row['via_paid'] == 'bank'){
														echo '<form name="paidStatusForm'.$row['userDetailId'].'" id="paidStatusForm'.$row['userDetailId'].'" action="'.$_SERVER['PHP_SELF'].'?mng_user=1" method="POST">
															<input type="checkbox" name="sel_pay_type" id="sel_pay_type'.$row['userDetailId'].'" class="chk_sub_chkbox">
															<select name="pay_type_status" id="pay_type_status'.$row['userDetailId'].'" class="chkSelValue">
																<option value="">Select Type</option>
																';
																$sel_bank_status = "SELECT * FROM tbl_bank_status";
																$course_bank_status = mysql_query($sel_bank_status) or die(mysql_error());
																if(mysql_num_rows($course_bank_status)>0){
																	while($row_bank_status = mysql_fetch_array($course_bank_status)){
																		echo '<option value="'.$row_bank_status['id'].'">'.$row_bank_status['type'].'</option>';
																	}
																}
														echo '</select><input type="hidden" name="userdtlpayerId" id="userdtlpayerId" value="'.$row['userDetailId'].'">
														</form>';
													} 
												echo '</td>
												<td>'; 
												$acc_no = "";
												$bank_name = "";
												$acc_date = "";
												$sel_bnk_dtl = "SELECT * FROM tbl_user_bnk_details WHERE user_id = '".$row['userId']."' AND user_detail_id = '".$row['userDetailId']."'";
												$query_bnk_dtl = mysql_query($sel_bnk_dtl) or die(mysql_error());

												if(mysql_num_rows($query_bnk_dtl) > 0){
													while($row_bnk_details = mysql_fetch_array($query_bnk_dtl)){
														$acc_no = $row_bnk_details['account_number'];
														$bank_name = $row_bnk_details['bank_name'];
														$acc_date = $row_bnk_details['date'];
													}
												}

												echo $acc_no.'</td>
												<td>'.$bank_name.'</td>
												<td>'.$acc_date.'</td>
												<td>';
													if($row['is_ebook']){
														echo 'Yes';
													}else{
														echo 'No';
													}
												echo '</td>
												<td>';
													if($row['is_ebook']){
														echo '<form name="ebook_counter_value" id="ebook_counter_value"><input type="text" name="count_val" id="count_val" value="'.$row['ebook_download_time'].'"><input type="hidden" name="user_id_val" id="user_id_val" value="'.$row['userId'].'"><input type="hidden" name="user_dtl_id_val" id="user_dtl_id_val" value="'.$row['userDetailId'].'"></form>';
													}else{
														echo $row['ebook_download_time'];
													}
												echo '</td>
												<td>'; if($row['is_refund']){
														echo '<span class="label label-success">Yes</span>';
													}else{
														echo '<span user_id= "'.$row['userDetailId'].'" class="label label-default refund_no ">No</span>';
													} 
												echo '</td>
												<td>';
													if($row['is_active']){
														echo '<span class="label label-success">Active</span>';
													}else{
														echo '<span class="label label-default">Inactive</span>';
													}

													echo '<br /><br />
														<a class="btn btn-info" href="'.$_SERVER['PHP_SELF'].'?mng_user=1&act_id='.$row['userId'].'&d=1" title="Delete User">
															<i class="ace-icon fa fa-trash-o bigger-120 "></i> 
														</a>';

												echo '</td>
												<td>';
													if(!$row['is_active']){
														echo '
														<a class="btn btn-info" href="'.$_SERVER['PHP_SELF'].'?mng_user=1&act_id='.$row['userId'].'" title="Activate">
															<i class="fa fa-edit "></i> 
														</a>';
													}
													echo '
													<a class="btn btn-danger" href="'.$_SERVER['PHP_SELF'].'?mng_user=1&del_id='.$row['userId'].'" title="Delete">
														<i class="fa fa-trash-o "></i> 
													</a>
												</td>
											</tr>
										';
										$i++;
									}
								}else{
									echo '<tr>
										<td colspan="9" style="text-align:center;">No Records Found</td>
									</tr>';
								}

							?>						
						  </tbody>
					  </table>  
					  <?php		
						echo '<ul class="pagination">';
						if($rec_count>$rec_limit){
							if( $page > 0 && $left_rec > $rec_limit)
							{
							   $last = $page - 2;
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?mng_user=1&page='.$last.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Prev</a></li> ';
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?mng_user=1&page='.$page.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Next</a></li>';
							}
							else if( $page == 0 )
							{
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?mng_user=1&page='.$page.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Next</a></li>';
							}
							else if( $left_rec < $rec_limit || $left_rec == $rec_limit )
							{
							   $last = $page - 2;
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?mng_user=1&page='.$last.'&srch_value='.$value.'&sel_get_limit='.$rec_limit.'" class="anc_click">Prev</a></li>';
							}
						}
						echo '</ul>';
						?>
					</div>
				</div>
			</div><!--/col-->

		</div><!--/row-->
	</div><!-- End Box Div-->
</div><!-- End Middle Content Div-->
<?php include_once('footer.php'); ?>
<script>
	$ = jQuery.noConflict();
	$(document).ready(function(){	
		$('.chkSelValue').click(function(){
			var payer_id = $(this).next('input#userdtlpayerId').val();
			if($('#sel_pay_type'+payer_id+':checked').length != 0){	
				$('#sel_pay_type'+payer_id+'').trigger('click');
			}
		});

		$('.chk_sub_chkbox').live('click',function(){
			var payer_id = $(this).next().next('input#userdtlpayerId').val();			
			if($('#sel_pay_type'+payer_id+':checked').length != 0){				
				if($('#pay_type_status'+payer_id).val() == ''){
					alert('Plaese Select One payment Method');
					$('#sel_pay_type'+payer_id+'').trigger('click');
				}
				else{					
					var user_dtl_pyr_id = payer_id;
					var status_pay_type = $('#pay_type_status'+user_dtl_pyr_id).val();
					
					var drftchqval = $('form#paidStatusForm'+payer_id+' input#drft_chq_no').val();
					$('form#paidStatusForm'+payer_id+' input#drft_chq_no').remove();
					
					if($('#pay_type_status'+payer_id).val() == 2 || $('#pay_type_status'+payer_id).val() == 3){
						
						$('form#paidStatusForm'+payer_id).append('<input type="text" name="drft_chq_no" id="drft_chq_no" value="">');
						$('form#paidStatusForm'+payer_id+' input#drft_chq_no').val(drftchqval);
						$('#sel_pay_type'+payer_id+'').trigger('click');
					}

					if($('form#paidStatusForm'+payer_id+' input#drft_chq_no').val() == ''){
						alert('Please Fill The VAlue');	
					}else{
						var send_drft_chq = '';
						if($('form#paidStatusForm'+payer_id+' input#drft_chq_no').val()){
							send_drft_chq = $('form#paidStatusForm'+payer_id+' input#drft_chq_no').val();
						}
						$('form#paidStatusForm'+user_dtl_pyr_id).css('display','none');
						$('form#paidStatusForm'+user_dtl_pyr_id).parent('td').append('<img src ="../img/loading.gif">');
						$.ajax({	
						type: 'POST',					
						data: {pay_type_status:status_pay_type,userdtlpayerId:user_dtl_pyr_id,chq_drft_val:send_drft_chq},
						dataType: 'json',
						url: '../classess/user_paid_type.php',
						success: 
							function(j)
							{
								//console.log(j);
								var scssHtml ='';
								$('form#paidStatusForm'+user_dtl_pyr_id).next('img').remove();
								$('form#paidStatusForm'+user_dtl_pyr_id).css('display','block');
								//$('form#paidStatusForm'+user_dtl_pyr_id).parent('td').html(j);
							}
						});	
					}
					/*$.ajax({	
					type: 'POST',					
					data: {pay_type_status:status_pay_type,userdtlpayerId:user_dtl_pyr_id},
					dataType: 'json',
					url: '../classess/user_paid_type.php',
					success: 
						function(j)
						{
						console.log(j);
							$('form#paidStatusForm'+user_dtl_pyr_id).parent('td').html(j);
						}
					}); */
				}
			}
		});

		$('.close').click(function(){
			$('.alert-info').addClass('hide');
		});

		$('#count_val').keyup(function(){
			var val = $('#count_val').val();
			var j_user_id = $('#user_id_val').val();
			var j_user_dtl_id = $('#user_dtl_id_val').val();
			if(val>0){
				$.ajax({	
				type: 'POST',					
				data: {field_value:val,pst_user_id:j_user_id,pst_user_dtl_id:j_user_dtl_id},
				dataType: 'json',
				url: '../classess/ebook_download_time.php',
				success: 
					function(j)
					{
						$('#count_val').val(val);
					}
				});
			}
		});
		
		$('.refund_no').live('click',function(){   
			
			var user_id = $(this).attr('user_id');
			//alert (user_id);
			
		$.ajax({ 

			type: 'POST',
			
			data: {pst_user_id:user_id},

			dataType: 'json',

			url: 'ajex_manage_user.php',

			success: 

			function(ninja){     

				window.location.reload();

			}

		});

	});
	
	});
		
	
</script>